/** @file    EncodersRaw_Core.cpp
 *  @author  Alessandro Polo
 *  @version $Id: EncodersRaw_Core.cpp 2739 2010-07-07 16:29:01Z alex $
 *  @brief
 * The header for this class can be found in EncodersRaw_Core.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include <framework/persistence/raw/EncodersRaw_Core.h>

 #include <core/ObjectAllocator.h>


namespace wosh {
 namespace persistence {
  namespace raw {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_ENCODER_RAW_BIND(wosh::persistence::raw::EncoderRaw_Variant, "wosh::Variant", 0.0, _static_EncoderRaw_Variant )

WRESULT EncoderRaw_Variant::serializeTo( const wosh::Variant* object, FILE* fout, int encoder_options ) {
	if ( object == NULL || fout == NULL ) return WRET_ERR_PARAM;

	std::string varStr = object->toString();
	std::string varStrSize = Utilities::toString((int)varStr.size());
	fputs(varStrSize.c_str(), fout);
	fputc(0, fout);
	fputs(varStr.c_str(), fout);

	return WRET_OK;
 }

WRESULT EncoderRaw_Variant::deserializeFrom( wosh::Variant* object, FILE* fin ) {
	if ( object == NULL || fin == NULL ) return WRET_ERR_PARAM;
	object->clear();

	char varStrSize[32];
	memset(varStrSize, 0, 32);
	fgets(varStrSize , 32, fin);
	int varStrSizeInt = Utilities::fromString<int>(varStrSize, 0);
	if ( varStrSizeInt > 0 ) {
		char* buffer = new char[varStrSizeInt];
		size_t result = fread(buffer,1,varStrSizeInt,fin);
		if ( result != varStrSizeInt ) {
			delete [] buffer;
			return WRET_ERR_INTERNAL;
		 }
		object->fromString( buffer );
		delete [] buffer;
	 }

	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

  }; // namespace raw
 }; // namespace persistence
}; // namespace wosh
